package com.crayon.account.fragmentspop;

import android.os.Bundle;
import android.view.View.OnClickListener;

import com.crayon.account.dao.DAOCategory;
import com.crayon.account.dao.DataBaseManager;
import com.crayon.account.data.ItemList;

import java.io.Serializable;

public class FragmentPopEditCate extends FragmentPopEditRoot implements OnClickListener{

	DAOCategory daoCategory;
	public static FragmentPopEditCate newInstance(ItemList item) {
		FragmentPopEditCate f = new FragmentPopEditCate();
		// Supply num input as an argument.
		Bundle bundle = new Bundle();
		if(item!=null){
			bundle.putSerializable("item", item);
			bundle.putSerializable("dao", (Serializable)item.getTag());
		}
		f.setArguments(bundle);
		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(getArguments()!=null){
			daoCategory = (DAOCategory)getArguments().getSerializable("dao");
		}
	}

	@Override
	public void OnEventNewCreat() {
		// TODO Auto-generated method stub
		daoCategory.setId(0);
		daoCategory.setUniqueName(null);
		daoCategory.setSummary(null);
	}

	@Override
	public void OnEventDelete() {
		DataBaseManager baseManager = DataBaseManager.getHelper(getActivity());
		try {
			baseManager.getDAOCategory().delete(daoCategory);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public boolean OnEventUpdate(String cate) {
		DataBaseManager baseManager = DataBaseManager.getHelper(getActivity());
		try {
			if(daoCategory!=null){
				daoCategory.setUniqueName(cate);
				daoCategory.setSummary(cate);
				if(daoCategory.getId()>0){
					baseManager.getDAOCategory().update(daoCategory);

				}else{
					baseManager.getDAOCategory().create(daoCategory);
				}
				return true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}
}